package com.maschinenstuermer.profiler.transform;

import java.util.logging.Logger;

import org.ow2.util.asm.ClassAdapter;
import org.ow2.util.asm.ClassVisitor;
import org.ow2.util.asm.MethodVisitor;

class ProfilingClassAdapter extends ClassAdapter {
	private static Logger logger = Logger.getLogger(ProfilingClassAdapter.class.getName());
	
	private String className;

	public ProfilingClassAdapter(final ClassVisitor cv) {
		super(cv);
	}

	@Override
	public void visit(final int version, final int access, final String name, final String signature,
			final String superName, final String[] interfaces) {
		super.visit(version, access, name, signature, superName, interfaces);
		className = name.replace("/", ".");
		logger.info("Tracing:" + className);
	}
	
	@Override
	public MethodVisitor visitMethod(final int access, final String name, final String description,
			final String signature, final String[] exceptions) {
		final MethodVisitor mv = cv.visitMethod(access, name, description, signature, exceptions);
		boolean isConstructor = name.contains("<") || mv == null;
		if (isConstructor) {
			return mv;
		} else {
			final MethodVisitor adviceAdapter = new ProfilingAdviceAdapter(mv, access, name, description, className);
			return adviceAdapter;
		}
	}
}
